/* MainView.java
 * Author: liuex
 * 2007-12-19 ����02:11:06
 *
 * Note: 
 */
package lex.proxyverfier;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Formatter;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;

import org.lex.swing.ContainerFactory;
import org.lex.swing.TextComponentHelper;
import org.lex.swing.widgets.EnhancedTextArea;

import lex.proxyverfier.verifier.VerifyResult;

public class MainContentPane extends JPanel {
	private JSplitPane splitPane;
	private EnhancedTextArea logTextArea;
	private JTable proxyTable;
	private JToolBar toolBar;
	private JPanel centerPane;
	private JButton verifyButton;
	private JButton removeButton;
	private JButton addButton;
	// =========================
	private ProxyTableModel proxyTableModel = null;

	public MainContentPane() {
		super();
		this.initialize();
		this.hookActions();
	}

	public Formatter getFormatter() {
		return TextComponentHelper.getFormatter(this.getLogTextArea());
	}

	public ProxyTableModel getProxyTableModel() {
		if (null == proxyTableModel) {
			this.proxyTableModel = new ProxyTableModel();
		}
		return this.proxyTableModel;
	}

	public ProxyServer2 getSelectItem() {
		int row = this.getProxyTable().getSelectedRow();
		if (-1 == row)
			return null;
		row = this.proxyTable.convertRowIndexToModel(row);
		return this.getProxyTableModel().getProxyServer(row);
	}

	public void removeSelectItem() {
		int row = this.getProxyTable().getSelectedRow();
		if (-1 == row)
			return;
		row = this.proxyTable.convertRowIndexToModel(row);
		this.getProxyTableModel().remove(row);
	}

	private void hookActions()
	{
		// toolbar action-listener
		final ActionMap am = ProxyVerifier.getApplication().getContext().getActionManager()
			.getActionMap();
		this.getAddButton().setAction( am.get( "add" ) );
		this.getRemoveButton().setAction( am.get( "remove" ) );
		this.getVerifyButton().setAction( am.get( "verify" ) );
		// if no-item selected, disable the "edit"&"remove" action
		this.getProxyTable().getSelectionModel().addListSelectionListener(
			new ListSelectionListener(){
				Action remove = am.get( "remove" );

				@Override
				public void valueChanged( ListSelectionEvent e )
				{
					int row = proxyTable.getSelectedRow();
					boolean enable = false;
					if( -1 != row )
						enable = true;
					remove.setEnabled( enable );
				}
			} );
		this.getProxyTable().addMouseListener( new MouseAdapter(){
			@Override
			public void mouseClicked( MouseEvent e )
			{
				if( e.getClickCount() > 1 )
				{
					int row = proxyTable.getSelectedRow();
					if( -1 != row )
						this.showStatistics( getProxyTableModel().getProxyServer( row ) );
				}
			}

			private String sep = "====================";

			private void showStatistics( ProxyServer2 server )
			{
				Formatter out = getFormatter();
				VerifyResult r = server.getVerifyResult();
				out.format( "%s %s statistics %s%n", sep, server.toString(), sep );
				out.format( "%d / %d URL successed%n", r.getValidSample(), r.getTotalSample() );
				out.format( "Average speed: %s%n", SpeedFormat.format( r.getAverageSpeed() ) );
				out.format( "%s%n", r.getDetailStatistics() );
			}
		} );
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new java.awt.Dimension(400, 293));
		this.add(getToolBar(), BorderLayout.NORTH);
		this.add(getCenterPane(), BorderLayout.CENTER);
	}

	private JTable getProxyTable() {
		if (proxyTable == null) {
			proxyTable = new JTable(this.getProxyTableModel());
			proxyTable.setName(this.getClass().getCanonicalName() + ".proxyTable");
			proxyTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			proxyTable.setAutoCreateRowSorter(true);
			int[] widths = new int[] { 30, 100, 50, 135, 50, 150 };
			TableColumnModel tcm = proxyTable.getColumnModel();
			for (int i = 0; i < widths.length; i++)
				tcm.getColumn(i).setPreferredWidth(widths[i]);
		}
		return proxyTable;
	}

	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
		}
		return addButton;
	}

	private JButton getRemoveButton() {
		if (removeButton == null) {
			removeButton = new JButton();
		}
		return removeButton;
	}

	private JButton getVerifyButton() {
		if (verifyButton == null) {
			verifyButton = new JButton();
		}
		return verifyButton;
	}

	private JPanel getCenterPane() {
		if (centerPane == null) {
			centerPane = new JPanel();
			centerPane.setLayout(new BorderLayout());
			centerPane.add(new FilterPane(), BorderLayout.NORTH);
			centerPane.add(this.getSplitPane(), BorderLayout.CENTER);
		}
		return centerPane;
	}

	private JSplitPane getSplitPane() {
		if (null == splitPane) {
			splitPane = ContainerFactory.createBorderlessSplitPane(5);
			splitPane.setName(this.getClass().getCanonicalName() + ".splitPane");
			splitPane.setResizeWeight(0.7D);
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			splitPane.setLeftComponent(new JScrollPane(getProxyTable()));
			splitPane.setBottomComponent(new JScrollPane(this.getLogTextArea()));
		}
		return splitPane;
	}

	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.add(getAddButton());
			toolBar.add(getRemoveButton());
			toolBar.addSeparator();
			toolBar.add(getVerifyButton());
		}
		return toolBar;
	}

	private EnhancedTextArea getLogTextArea() {
		if (null == logTextArea) {
			logTextArea = new EnhancedTextArea();
			logTextArea.setAutoScrollText(true);
		}
		return logTextArea;
	}
}
